<template>
  <div>
    <!-- 头(查询) -->
    <el-form :inline="true" :model="teacherQuery" class="demo-form-inline">
      <el-form-item label="教师名称">
        <el-input v-model="teacherQuery.name" placeholder="教师名称"></el-input>
      </el-form-item>
      <el-form-item label="教师头衔">
        <el-select v-model="teacherQuery.level" placeholder="教师头衔">
          <el-option label="高级讲师" :value="1"></el-option>
          <el-option label="首席讲师" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="teacherQuery.begin"
          type="date"
          placeholder="开始日期"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="teacherQuery.end"
          type="date"
          placeholder="结束时间"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList()">查询</el-button>
        <el-button type="default" @click="resetData()">清空</el-button>
      </el-form-item>
    </el-form>
    <!-- body -->
    <el-table :data="list" style="width: 100%">
      <el-table-column label="序号" type="index" width="50"></el-table-column>
      <el-table-column prop="name" label="讲师姓名" width="180"></el-table-column>
      <el-table-column prop="level" label="头衔" width="180">
        <template slot-scope="scope">{{ scope.row.level === 1 ? "高级讲师" : "首席讲师" }}</template>
      </el-table-column>
      <el-table-column prop="intro" label="讲师简介" width="180"></el-table-column>
      <el-table-column prop="career" label="讲师资历" width="180"></el-table-column>
      <el-table-column prop="avatar" label="讲师头像" width="180"></el-table-column>
      <el-table-column prop="sort" label="排序" width="180"></el-table-column>
      <el-table-column prop="gmtCreate" label="日期" width="180"></el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template slot-scope="scope">
          <router-link :to="'/teacher/edit/'+scope.row.id">
            <el-button type="primary" size="small">修改</el-button>
          </router-link>
          <el-button type="danger" size="small" @click="removeById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      background
      layout="prev, pager, next, jumper, total"
      @current-change="getList"
    />
  </div>
</template>



<script>
//首先引入js
import teacher from "@/api/edu/teacher";

export default {
  //写核心代码
  data() {
    //定义vue的 变量和初始值
    return {
      list: null, //查询后返回的集合
      page: 1, //当前页
      limit: 10, //每页记录数
      total: 0,
      teacherQuery: {} //条件对象
    };
  },
  created() {
    //在页面渲染之前执行
    this.getList();
  },
  methods: {
    //定义自己的方法,掉用teacher.js
    getList(page = 1) {
      this.page = page;
      teacher
        .getTeacherListPage(this.page, this.limit, this.teacherQuery)
        .then(response => {
          this.list = response.data.records;
          this.total = response.data.total;
        }) //请求成功
        .catch(error => {
          console.log(error);
        }); //请求失败
    },
    //删除讲师
    removeById(id) {
      this.$confirm("是否删除该教师?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        teacher.removeById(id).then(response => {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          //回到列表页面
          this.getList(this.page);
        });
      });
    },
    //清空
    resetData() {
      this.teacherQuery = {};
      this.getList();
    },
    handleClick(row) {
      console.log(row);
    }
  }
};
</script>
